package com.squirrel.michale;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/22 2:50 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode2105 {

    public int minimumRefill(int[] plants, int capacityA, int capacityB) {

        int refillCount = 0;
        int remainA = capacityA;
        int remainB = capacityB;
        int plantCount = plants.length;
        int left = 0;
        int right = plantCount - 1;
        while (left <= right) {
            if (left == right) {
                if (remainA == remainB) {
                    if (remainA < plants[left]) {
                        refillCount++;
                    }
                }else{
                    int max = remainA > remainB ? remainA : remainB;
                    if (max < plants[left]) {
                        refillCount++;
                    }
                }
                return refillCount;
            }
            if (remainA >= plants[left]) {
                remainA -= plants[left];
            }else {
                remainA = capacityA - plants[left];
                refillCount++;
            }

            if (remainB >= plants[right]) {
                remainB -= plants[right];
            }else{
                remainB = capacityB - plants[right];
                refillCount++;
            }

            left++;
            right--;


        }
        return refillCount;
    }

    public static void main(String[] args) {

        LeetCode2105 leetCode2105 = new LeetCode2105();

//        输入：plants = [2,2,3,3], capacityA = 5, capacityB = 5
//        输出：1

        int[] plants = new int[]{2, 2, 3, 3};
        int capacityA = 5, capacityB = 5;

        System.out.println(leetCode2105.minimumRefill(plants, capacityA, capacityB));

//        输入：plants = [2,2,3,3], capacityA = 3, capacityB = 4
//        输出：2

        int[] plants2 = new int[]{2, 2, 3, 3};
        int capacityA2 = 3, capacityB2 = 4;

        System.out.println(leetCode2105.minimumRefill(plants2, capacityA2, capacityB2));

//        输入：plants = [5], capacityA = 10, capacityB = 8
//        输出：0

        int[] plants3 = new int[]{5};
        int capacityA3 = 10, capacityB3 = 8;

        System.out.println(leetCode2105.minimumRefill(plants3, capacityA3, capacityB3));


        int[] plants5 = new int[]{1,2,4,4,5};
        int capacityA5 = 6, capacityB5 = 5;
        System.out.println(leetCode2105.minimumRefill(plants5, capacityA5, capacityB5));
    }
}
